package com.onec.service.trade.service.impl;

import cn.hutool.core.date.DateUtil;
import com.onec.service.api.dto.request.gateway.BizContent;
import com.onec.service.api.dto.response.customer.AgentInfoResp;
import com.onec.service.api.dto.response.customer.MerchantResp;
import com.onec.service.api.dto.response.route.RouteResp;
import com.onec.service.api.enums.StatusEnum;
import com.onec.service.api.enums.error.TradeErrorEnum;
import com.onec.service.api.exception.TradeException;
import com.onec.service.trade.dao.OnlineOrderRepository;
import com.onec.service.trade.entity.OnlineOrder;
import com.onec.service.trade.service.OnlineOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 交易订单实现类：
 *
 * @author onec
 */
@Service
public class OnlineOrderServiceImpl implements OnlineOrderService {

    @Autowired
    OnlineOrderRepository onlineOrderRepository;


    @Override
    public OnlineOrder createOrder(MerchantResp merchantResp, RouteResp routeResp, AgentInfoResp agentInfoResp,
                                   BizContent bizContent) {
        // 验证下游订单号是否存在
        if (onlineOrderRepository.updateOnlineOrderByExternalNum(DateUtil.date(), bizContent.getExternalNum()) > 0) {
            throw new TradeException(TradeErrorEnum.EXTERNAL_NUM_EXIST_ERROR);
        }
        // 交易订单创建成功：
        OnlineOrder onlineOrder = new OnlineOrder(merchantResp, agentInfoResp, bizContent, routeResp);
        onlineOrderRepository.save(onlineOrder);
        return onlineOrder;
    }

    @Override
    public OnlineOrder findBySerialNum(String serialNum) {
        return onlineOrderRepository.findBySerialNum(serialNum);
    }

    /**
     * 获取订单号！不处理状态
     *
     * @param serialNum
     * @return
     */
    @Override
    public OnlineOrder getAvailableBySerialNum(String serialNum) {
        OnlineOrder onlineOrder = this.findBySerialNum(serialNum);
        if (Optional.ofNullable(onlineOrder).isEmpty()) {
            throw new TradeException(TradeErrorEnum.SERIAL_NUM_NOT_EXIST_ERROR);
        }

        return onlineOrder;
    }

    @Override
    public OnlineOrder findByTradeNum(String tradeNum) {
        return onlineOrderRepository.findByTradeNum(tradeNum);
    }

    /**
     * tradenum 获取订单！
     *
     * @param tradeNum
     * @return
     */
    @Override
    public OnlineOrder getAvailableByTradeNum(String tradeNum) {
        OnlineOrder onlineOrder = this.findByTradeNum(tradeNum);
        if (Optional.ofNullable(onlineOrder).isEmpty()) {
            throw new TradeException(TradeErrorEnum.TRADE_NUM_NOT_EXIST_ERROR);
        }
        return onlineOrder;
    }

    /**
     * 处理交易订单问题！
     *
     * @param tradeNum
     * @param statusEnum
     * @param message
     * @return
     */
    @Override
    public OnlineOrder updateByTradeNum(String tradeNum, StatusEnum statusEnum, String message) {
        OnlineOrder onlineOrder = this.findByTradeNum(tradeNum);
        if (Optional.ofNullable(onlineOrder).isEmpty()) {
            throw new TradeException(TradeErrorEnum.TRADE_NUM_NOT_EXIST_ERROR);
        }
        onlineOrder.setStatus(statusEnum);
        onlineOrder.setRemarks(message);
        return onlineOrderRepository.save(onlineOrder);
    }
}
